// 小程序请求
var app = getApp();
const request = (apiUrl, data, options) => {
  let needLoad = {mask: false,show: false}
  options = options || {loading: needLoad }
  options.loading = options.loading ? options.loading : needLoad
  wx.showLoading({
    title: '加载中...',
    mask: options.loading.mask
  });
  let url = `${app.globalData.baseUrl}${apiUrl}`
  console.log('请求地址',  url)
  console.log('请求参数', data)
  // console.log('请求方式', options)
  return new Promise((resolve, reject) => {
    wx.request({
      url,
      data,
      // header: options.header || { 'content-type': 'application/json' },
      header: options.header || { 'Content-Type': 'application/x-www-form-urlencoded'},
      method: options.method || 'post',
      dataType: options.dataType || 'json',
      responseType: options.responseType || 'text',
      success: res => {
        resolve(res.data)
        console.log('响应成功', res.data)
        options.loading.show ? '' : wx.hideLoading();
      },
      fail: err => {
        reject(err)
        console.log('响应失败', err)
        options.loading.show ? '' : wx.hideLoading();
      }
    })
  })
}

export default request

